﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class CustomerMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((User)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }
            if (!IsPostBack)
            {
                ////bntbrowse.Enabled = false;
                ////bntgetdetails.Enabled = false;
                ////rbOption.Focus();
                BindGrid();
            }
        }

        public void BindGrid()
        {
            CustomerEntity custEntity = new CustomerEntity();
            List<Customer> lstCustomer = null;
            try
            {
                    lstCustomer = custEntity.GetCustomerDetailsByCustomerId("");

                    if (lstCustomer == null)
                    {
                        gvCustomer.DataSource = null;
                        gvCustomer.DataBind();
                    }
                    else
                    {
                        gvCustomer.DataSource = lstCustomer;
                        gvCustomer.DataBind();
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            List<Customer> lstCustomer = null;
            CustomerEntity custEntity = new CustomerEntity();
            Customer cust = new Customer();
            string resp = string.Empty;
            int is_Enabled = 0;
           
            if (chkIsenabled.Checked)
                is_Enabled = 1;
            
            ////if (rbOption.SelectedValue == "1")
            ////{

                lstCustomer = custEntity.GetCustomerDetailsByCustomerId(txtCustomerId.Text.Trim());

                if (lstCustomer != null)
                 {
                     cust = new Customer
                     {
                         CustomerID = txtCustomerId.Text.Trim(),
                         CustomerName = txtName.Text.Trim(),
                         Address1 = txtAddress1.Text.Trim(),
                         Address2 = txtAddress2.Text.Trim(),
                         City = txtCity.Text.Trim(),
                         State = txtState.Text.Trim(),
                         Country = txtCountry.Text.Trim(),
                         ZipCode = txtZipCode.Text.Trim(),
                         TelephoneNumber = txtPhone.Text.Trim(),
                         EmailAddress = txtEmail.Text.Trim(),
                         IsEnabled = Convert.ToBoolean(is_Enabled),
                         //IsActive = chkActive.Checked
                     };
                     int retval = custEntity.Modify(cust, ((User)Session["CurrentUserVO"]).UserID);

                     if (retval != 0)
                     {
                         //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer modified successfully!')</script>");
                         lblMessage.Text = "Customer modified successfully!";
                         btnClickPopup.Show();
                         ResetFields();
                     }
                     else
                     {
                         //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error Modifying Customer!')</script>");
                         lblMessage.Text = "Error Modifying Customer!";
                         btnClickPopup.Show();
                     }
                 }
                 else
                 {
                     //string customerID = System.DateTime.Now.ToString("MMddHHmmss");
                     cust = new Customer
                     {
                         CustomerID= txtCustomerId.Text.Trim(),
                         CustomerName= txtName.Text.Trim(),
                         Address1= txtAddress1.Text.Trim(),
                         Address2 = txtAddress2.Text.Trim(),
                         City= txtCity.Text.Trim(),
                         State= txtState.Text.Trim(),
                         Country =txtCountry.Text.Trim(),
                         ZipCode= txtZipCode.Text.Trim(),
                         TelephoneNumber= txtPhone.Text.Trim(),
                         EmailAddress= txtEmail.Text.Trim(),
                         IsEnabled= Convert.ToBoolean(is_Enabled),
                         //IsActive = chkActive.Checked
                     };
                     int retval = custEntity.Add(cust, ((User)Session["CurrentUserVO"]).UserID);

                     if (retval != 0)
                     {
                         //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer added successfully!')</script>");
                         lblMessage.Text = "Customer added successfully!";
                         btnClickPopup.Show();
                         
                         ResetFields();
                     }
                     else
                     {
                         //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error adding Customer!')</script>");
                         lblMessage.Text = "Error adding Customer!";
                         btnClickPopup.Show();
                     }
                     
                 }
                BindGrid();
            /*}
            else
            {
                lstCustomer = custEntity.GetCustomerDetailsByCustomerId(txtCustomerId.Text.Trim());

                if (lstCustomer == null)
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer does not exist!')</script>");
                    lblMessage.Text = "Customer does not exist!";
                    btnClickPopup.Show();
                }
                else
                {
                    cust = new Customer
                    {
                        CustomerID = txtCustomerId.Text.Trim(),
                        CustomerName = txtName.Text.Trim(),
                        Address1 = txtAddress1.Text.Trim(),
                        Address2 = txtAddress2.Text.Trim(),
                        City = txtCity.Text.Trim(),
                        State = txtState.Text.Trim(),
                        Country = txtCountry.Text.Trim(),
                        ZipCode = txtZipCode.Text.Trim(),
                        TelephoneNumber = txtPhone.Text.Trim(),
                        EmailAddress = txtEmail.Text.Trim(),
                        IsEnabled = Convert.ToBoolean(is_Enabled),
                        //IsActive = chkActive.Checked
                    };
                    int retval = custEntity.Modify(cust, ((User)Session["CurrentUserVO"]).UserID);

                    if (retval != 0)
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer modified successfully!')</script>");
                        lblMessage.Text = "Customer modified successfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error Modifying Customer!')</script>");
                        lblMessage.Text = "Error Modifying Customer!";
                        btnClickPopup.Show();
                    }
                
                }

            }*/
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        private void ResetFields()
        {
            ////rbOption.SelectedValue = "1";
            ////bntgetdetails.Enabled = false;
            ////bntbrowse.Enabled = false;
            txtCustomerId.Text = "";
            txtName.Text = "";
            txtAddress1.Text = "";
            txtAddress2.Text = "";
            txtCity.Text = "";
            txtState.Text = "";
            txtCountry.Text = "";
            txtZipCode.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
            chkIsenabled.Checked = true;
            btnSubmit.Text = "Add";
            //chkActive.Checked = true;
        }

        ////protected void bntbrowse_Click(object sender, EventArgs e)
        ////{
        ////    string path;
        ////    path = "CustomerMaster.aspx";
        ////    // txtUserId.Text - Get the value ctl00$BodyHolder$txtUserId from Unique Id of the control txtUserId
        ////    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<SCRIPT>window.open('SearchCustomer.aspx?textbox=" + HttpUtility.UrlEncode(txtCustomerId.Text) + "&PageRequest= " + path + "','cal','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        ////}

        ////protected void bntgetdetails_Click(object sender, EventArgs e)
        ////{
        ////    //Customer cust = new Customer();
        ////    CustomerEntity custEntity = new CustomerEntity();
        ////    List<Customer> lstCustomer = null;
        ////    try
        ////    {
        ////        if (txtCustomerId.Text.Trim() != "")
        ////        {
        ////            lstCustomer = custEntity.GetCustomerDetailsByCustomerId(txtCustomerId.Text.Trim());

        ////            if (lstCustomer == null)
        ////            {
        ////                lblMessage.Text = "No record found for this customer!";
        ////                btnClickPopup.Show();
        ////            }
        ////            else
        ////            {
        ////                if (lstCustomer.Count == 1)
        ////                {
        ////                    foreach (Customer cust in lstCustomer)
        ////                    {
        ////                        ResetFields();

        ////                        txtCustomerId.Text = cust.CustomerID;
        ////                        txtName.Text = cust.CustomerName;
        ////                        txtAddress1.Text = cust.Address1;
        ////                        txtAddress2.Text = cust.Address2;
        ////                        txtCity.Text = cust.City;
        ////                        txtState.Text = cust.State;
        ////                        txtCountry.Text = cust.Country;
        ////                        txtZipCode.Text = cust.ZipCode;
        ////                        txtPhone.Text = cust.TelephoneNumber;
        ////                        txtEmail.Text = cust.EmailAddress;
        ////                        chkIsenabled.Checked = cust.IsEnabled;
        ////                        //chkActive.Checked = cust.IsActive;
        ////                        rbOption.SelectedValue = "2";
        ////                    }
        ////                }
                        
        ////            }
        ////        }
        ////        else
        ////        {
        ////            lblMessage.Text = "Enter Customer ID!";
        ////            btnClickPopup.Show();

        ////        }

        ////    }
        ////    catch
        ////    {
        ////        throw;
        ////    }
        ////    finally
        ////    {
        ////    }
        ////}

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        ////protected void rbOption_SelectedIndexChanged(object sender, EventArgs e)
        ////{
        ////    if (rbOption.SelectedValue.Trim() == "1")
        ////    {
        ////        bntbrowse.Enabled = false;
        ////        bntgetdetails.Enabled = false;
        ////    }
        ////    else
        ////    {
        ////        bntbrowse.Enabled = true;
        ////        bntgetdetails.Enabled = true;
        ////    }
        ////}

        //protected void gvCustAccount_RowEditing(object sender, GridViewEditEventArgs e)
        //{
        //    gvCustomer.EditIndex = e.NewEditIndex;
        //    BindGrid();
        //}

        //protected void gvCustAccount_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        //{
        //    gvCustomer.EditIndex = -1;
        //    BindGrid();
        //}
       
        //protected void gvCustAccount_RowUpdating(object sender, GridViewUpdateEventArgs e)
        //{
        //    int retval = 0;
        //    try
        //    {
        //        CustomerEntity custEntity = new CustomerEntity();
        //        Customer cust;
        //        cust = new Customer
        //        {
        //            CustomerID = Convert.ToString(gvCustomer.Rows[e.RowIndex].Cells[1].Text),
        //            CustomerName = Convert.ToString(((TextBox)gvCustomer.Rows[e.RowIndex].Cells[2].Controls[0]).Text),
        //            Address1 = Convert.ToString(((TextBox)gvCustomer.Rows[e.RowIndex].Cells[3].Controls[0]).Text),
        //            Address2 = Convert.ToString(((TextBox)gvCustomer.Rows[e.RowIndex].Cells[4].Controls[0]).Text),
        //            City = Convert.ToString(((TextBox)gvCustomer.Rows[e.RowIndex].Cells[5].Controls[0]).Text),
        //            State = Convert.ToString(((TextBox)gvCustomer.Rows[e.RowIndex].Cells[6].Controls[0]).Text),
        //            ZipCode = Convert.ToString(((TextBox)gvCustomer.Rows[e.RowIndex].Cells[7].Controls[0]).Text),
        //            TelephoneNumber = Convert.ToString(((TextBox)gvCustomer.Rows[e.RowIndex].Cells[8].Controls[0]).Text),
        //            EmailAddress = Convert.ToString(((TextBox)gvCustomer.Rows[e.RowIndex].Cells[9].Controls[0]).Text),
        //            IsEnabled = Convert.ToBoolean(((CheckBox)gvCustomer.Rows[e.RowIndex].Cells[10].Controls[0]).Checked),
        //            //IsActive = chkActive.Checked
        //        };
        //        retval = custEntity.Modify(cust, ((User)Session["CurrentUserVO"]).UserID);

        //        if (retval != 0)
        //        {
        //            lblMessage.Text = "Customer modified successfully!";
        //            btnClickPopup.Show();
        //        }
        //        else
        //        {
        //            lblMessage.Text = "Error Modifying Customer!";
        //            btnClickPopup.Show();
        //        }

        //    }
        //    catch
        //    {

        //    }
        //    gvCustomer.EditIndex = -1;
        //    BindGrid();
        //}

        protected void gvCustAccount_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int retval = 0;
            try
            {
                Label custId = ((Label)gvCustomer.Rows[e.RowIndex].Cells[1].FindControl("lblCustomerID"));
                CustomerEntity custEntity = new CustomerEntity();
                retval = custEntity.DeleteCustomer(Convert.ToString(custId.Text));
                if (retval != 0)
                {
                    lblMessage.Text = "Customer deleted successfully!";
                    btnClickPopup.Show();
                }
                else
                {
                    lblMessage.Text = "Error in deleting customer detail!";
                    btnClickPopup.Show();
                }
            }
            catch
            {

            }
            BindGrid();
        }

        //protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{

        //    GridView1.PageIndex = e.NewPageIndex;

        //    gvbind();

        //}


    }
}